/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.player.VideoPlayer;
import com.yc.video.ui.view.BasisVideoController;

import org.yczbj.ycvideoplayer.ConstantVideo;
import org.yczbj.ycvideoplayer.IntentKeys;
import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.utils.ImageLoader;

/**
 * 3.1 最简单的播放视频，也是最常见
 *
 * @since 2021-05-10
 */
public class NormalAbility extends BaseAbility implements Component.ClickedListener {
    private VideoPlayer mVideoPlayer;
    private Button mBtnScaleNormal;
    private Button mBtnScale169;
    private Button mBtnScale43;
    private Button mBtnScaleFull;
    private Button mBtnScaleOriginal;
    private Button mBtnScaleCrop;
    private Button mBtnCrop;
    private Button mBtnGif;

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_normal;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);

        initFindViewById();
        initVideoPlayer();
    }

    private void initFindViewById() {
        mVideoPlayer = (VideoPlayer) findComponentById(ResourceTable.Id_video_player);
        mBtnScaleNormal = (Button) findComponentById(ResourceTable.Id_btn_scale_normal);
        mBtnScale169 = (Button) findComponentById(ResourceTable.Id_btn_scale_169);
        mBtnScale43 = (Button) findComponentById(ResourceTable.Id_btn_scale_43);
        mBtnScaleFull = (Button) findComponentById(ResourceTable.Id_btn_scale_full);
        mBtnScaleOriginal = (Button) findComponentById(ResourceTable.Id_btn_scale_original);
        mBtnScaleCrop = (Button) findComponentById(ResourceTable.Id_btn_scale_crop);
        mBtnCrop = (Button) findComponentById(ResourceTable.Id_btn_crop);
        mBtnGif = (Button) findComponentById(ResourceTable.Id_btn_gif);
    }

    private void initVideoPlayer() {
        String url = getIntent().getStringParam(IntentKeys.URL);
        if (url == null || url.length() == 0) {
            url = ConstantVideo.VideoPlayerList.get(0);
        }
        // 创建基础视频播放器，一般播放器的功能
        mController = new BasisVideoController(this);
        // 设置控制器
        mVideoPlayer.setController(mController);
        // 设置视频播放链接地址
        mVideoPlayer.setUrl(url);
        // 开始播放
        mVideoPlayer.start();
        // 这个延迟方法有问题 如果延迟300毫秒播放页面会出现闪一下 , 给50就不会
        // 设置视频背景图
        ImageLoader.loadImage(getContext(), ResourceTable.Media_image_default, mController.getThumb());
    }

    /**
     * 初始化监听器
     */
    protected void initListener() {
        mBtnScaleNormal.setClickedListener(this);
        mBtnScale169.setClickedListener(this);
        mBtnScale43.setClickedListener(this);
        mBtnScaleFull.setClickedListener(this);
        mBtnScaleOriginal.setClickedListener(this);
        mBtnScaleCrop.setClickedListener(this);
        mBtnCrop.setClickedListener(this);
        mBtnGif.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        if (component == mBtnScale169) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_16_9);
        } else if (component == mBtnScaleNormal) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_DEFAULT);
        } else if (component == mBtnScale43) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_4_3);
        } else if (component == mBtnScaleFull) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_MATCH_PARENT);
        } else if (component == mBtnScaleOriginal) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_ORIGINAL);
        } else if (component == mBtnScaleCrop) {
            mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_CENTER_CROP);
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (mVideoPlayer != null) {
            // 从后台切换到前台，当视频暂停时或者缓冲暂停时，调用该方法重新开启视频播放
            mVideoPlayer.resume();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (mVideoPlayer != null) {
            // 从前台切到后台，当视频正在播放或者正在缓冲时，调用该方法暂停视频
            mVideoPlayer.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoPlayer != null) {
            // 销毁页面，释放，内部的播放器被释放掉，同时如果在全屏、小窗口模式下都会退出
            mVideoPlayer.release();
        }
    }

    @Override
    protected void onBackPressed() {
        // 处理返回键逻辑；如果是全屏，则退出全屏；如果是小窗口，则退出小窗口
        ControlWrapper.defaultValue();
        if (mVideoPlayer == null || !mVideoPlayer.onBackPressed()) {
            super.onBackPressed();
        }
    }
}
